package com.hedl.elasticsearch.elastic;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Result;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.bulk.CreateOperation;
import com.hedl.elasticsearch.po.User;
import com.hedl.elasticsearch.service.impl.ElasticsearchServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Big Watermelon
 * @date 2023/10/30/22:41
 */
@SpringBootTest
public class ElasticsearchTestDoc {

    public final String ELASTIC_NAME = "watermelon";

    @Resource
    private ElasticsearchServiceImpl elasticsearchService;

    @Resource
    private ElasticsearchClient elasticsearchClient;

    //文档操作
    @Test
    public void operationDocument() throws IOException {
        User user = new User("大西瓜","男",23);
        CreateRequest createRequest = new CreateRequest.Builder<User>()
                .index(ELASTIC_NAME)
                .id("1001")
                .document(user)
                .build();
        elasticsearchClient.create(createRequest);
    }

    @Test
    public void lambdaDocument1() throws IOException {
        //文档操作，使用的lambda操作
        User user = new User("大西瓜","男",23);
        CreateResponse createResponse = elasticsearchClient.create(x -> x.index(ELASTIC_NAME).id("1001").document(user));
        System.out.println(createResponse);
    }

    @Test
    public void bulkDate() throws IOException {
        List<BulkOperation> operations = new ArrayList<>();
        for (int i = 1; i <= 5;i++){
            CreateOperation<User> optObj = new CreateOperation.Builder<User>()
                    .index(ELASTIC_NAME)
                    .id("200"+i)
                    .document(new User("小西瓜"+i,"女"+i,23))
                    .build();

            BulkOperation opt = new BulkOperation.Builder().create(optObj).build();

            operations.add(opt);
        }
        //批量添加数据
        BulkRequest bulkRequest = new BulkRequest.Builder().operations(operations).build();
        BulkResponse bulk = elasticsearchClient.bulk(bulkRequest);
        System.out.println("批量添加："+bulk);
    }

    //文档删除
    @Test
    public void deleteDoc() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest.Builder()
                .index(ELASTIC_NAME)
                .id("2002")
                .build();
        DeleteResponse delete = elasticsearchClient.delete(deleteRequest);
        System.out.println(""+delete);
    }

    @Test
    public void lambdaDocument2() throws IOException {
        //这里删除一定要使用id加索引，删除的是索引里面的信息，而不是索引本身
        DeleteResponse delete = elasticsearchClient.delete(x -> x.index(ELASTIC_NAME).id("1001"));
        System.out.println(delete);
    }

    //lambda的文档操作
    @Test
    public void lambdaDocument3() throws IOException {
        User user = new User("大西瓜你在做什么呢","男",23);

        //result()这个不加也可以，只是想看返回结果而已
        Result result = elasticsearchClient.create(
                req -> req.index(ELASTIC_NAME).id("3001").document(user)).result();
        System.out.println(result);
    }

    //批量添加数据
    @Test
    public void lambdaBulkDocument() throws IOException {
        List<User> users = new ArrayList<>();
        for (int i = 1; i <= 5;i++){
            users.add(new User("小西瓜","女",23+i));

        }
        elasticsearchClient.bulk(
                req -> {
                    users.forEach(
                            user -> {
                                req.operations(
                                        b -> b.create(
                                                d -> d.index(ELASTIC_NAME).id(user.getAge().toString()).document(user)
                                        )
                                );
                            }
                    );
                    return req;
                }
        );
    }

    @Test
    public void deleteDocument() throws IOException {
        elasticsearchClient.delete(req -> req.index(ELASTIC_NAME).id("2001"));
    }

    //查询文档
    @Test
    public void queryDocument() throws IOException {

        MatchQuery matchQuery = new MatchQuery.Builder()
                .field("name").query("小西瓜")
                .build();

        Query query = new Query.Builder()
                .match(matchQuery)
                .build();

        SearchRequest searchRequest = new SearchRequest.Builder()
                .query(query)
                .build();

        SearchResponse<Object> search = elasticsearchClient.search(searchRequest, Object.class);
        System.out.println(search);
    }

    @Test
    public void lambdaQueryDocument1() throws IOException {
        SearchResponse<Object> search = elasticsearchClient.search(
                sr -> sr.query(
                        q -> q.match(
                                mq -> mq.field("name").query("大西瓜")
                        )), Object.class);
        System.out.println(search);
    }

    //两个任选一个，写法不同，结果一样
    @Test
    public void lambdaQueryDocument2() throws IOException {
        SearchResponse<Object> search = elasticsearchClient.search(
                sr -> {
                    sr.query(
                            q -> q.match(
                                    m -> m.field("name").query("大西瓜")
                            )
                    );
                    return sr;
                }, Object.class);
        System.out.println(search);
    }
}
